<script setup lang="ts">
import { getAllMenuApi } from "@/api/menu";
import { getRoleListApi, removeRoleApi } from "@/api/role";
import { onMounted, ref } from "vue";
import RoleForm from "./component/RoleForm.vue";
const tableData = ref([]);
const drawer = ref(false);

const RoleFormRef = ref<any>(null);

const menuData = ref<any>([]);

const roleData = ref<any>([]);
// 获取数据
const getTableData = async () => {
  const res = await getRoleListApi();
  tableData.value = res;
  roleData.value = res.map((item: any) => {
    return {
      value: item.id,
      label: item.title,
    };
  });
};

// 获取菜单数据
const getMenuList = async () => {
  const res = await getAllMenuApi({ title: "" });
  tableData.value = res;
  // const menuTree = (treeData: any) => {
  //   return treeData.map((item: any) => {
  //     return {
  //       value: item.id,
  //       label: item.meta.title,
  //       children: item.children?.length ? menuTree(item.children) : [],
  //     };
  //   });
  // };
  // const menuTreeData = menuTree(res);
  // console.log(menuTreeData);
  menuData.value = res;
};

onMounted(() => {
  getTableData();
  getMenuList();
});

const saveRole = () => {
  RoleFormRef.value.open();
  drawer.value = true;
};

const removeRole = async (id: string) => {
  console.log(id);
  const ret = await removeRoleApi({ id });
  console.log(ret);
};

const updateRole = (row: any) => {
  RoleFormRef.value.open(JSON.parse(JSON.stringify(row)));
  drawer.value = true;
};
</script>

<template>
  <RoleForm ref="RoleFormRef" :menuData="menuData" />
  <div>
    <h1>角色</h1>
    <div class="table-top-btn">
      <el-button type="primary" size="small" @click="saveRole">添加</el-button>
      <el-button type="default" size="small" @click="getTableData"
        >刷新</el-button
      >
    </div>
    <!-- <el-drawer v-model="drawer" title="I am the title" :with-header="false">

    </el-drawer> -->
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="title" label="标题" width="140" />
      <el-table-column prop="name" label="名称" />
      <el-table-column width="150">
        <template #default="scope">
          <el-button
            type="danger"
            size="small"
            @click="removeRole(scope.row.id)"
            >删除</el-button
          >
          <el-button type="primary" size="small" @click="updateRole(scope.row)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<style scoped lang="less">
.table-top-btn {
  margin: 5px 20px;
}
</style>
